package com.itheima.dao.impl;

import com.itheima.dao.IStudentDao;
import com.itheima.domain.Student;

import java.util.ArrayList;

public class StudentDaoImpl implements IStudentDao {
    private static ArrayList<Student> list= new ArrayList<>();

    static {
        Student wu = new Student("0001", "吴亦凡", 31, "1991-01-01");
        Student lu = new Student("0002", "鹿晗", 31, "1991-01-01");
        Student huang = new Student("0003", "黄子韬", 31, "1991-01-01");
        Student zhang = new Student("0004", "张艺兴", 31, "1991-01-01");
        list.add(wu);
        list.add(lu);
        list.add(huang);
        list.add(zhang);
    }
    @Override
    public void addStudent(Student student) {
        list.add(student);
    }

    @Override
    public void deleteStudent(String sid) {
        int index = getIndex(sid);
        list.remove(index);
    }

    @Override
    public void updateStudent(String sid, Student newStu) {
        int index = getIndex(sid);
        list.set(index,newStu);
    }

    @Override
    public ArrayList<Student> findAllStudents() {
        return list;
    }

    @Override
    public int getIndex(String sid) {
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if(student!=null&&student.getSid().equals(sid)){
                return i;
            }
        }
        return -1;
    }
}
